<template>
  <div class="fee-summary-main">
    <div class="fee-summary-content">
      <template v-if="feeTreeStData.length > 0">
        <div class="fee-item" v-for="(item, index) in feeTreeStData" :key="index">
          <div class="fee-item-field flex justify-between" :class="[item.childrenToggle ? 'children-show' : '']">
            <div
              class="fee-name"
              :class="[item.children ? 'fee-name-toggle' : '']"
              @click="() => (item.children ? (item.childrenToggle = !item.childrenToggle) : '')"
            >
              {{ item.name }}
              <component
                class="fee-name-toggle-icon"
                v-if="item.children"
                :is="item.childrenToggle ? 'icon-caret-up' : 'icon-caret-down'"
              />
            </div>
            <div class="fee-value">{{ unitVal(item.value) }}</div>
          </div>
          <div class="fee-item-children" v-if="item.children && item.childrenToggle">
            <div class="fee-item-field flex justify-between" v-for="(childItem, childIndex) in item.children" :key="childIndex">
              <div class="fee-name">{{ childItem.name }}</div>
              <div class="fee-value">{{ unitVal(childItem.value) }}</div>
            </div>
          </div>
          <a-divider v-if="index < feeTreeStData.length - 1" style="margin: 0" />
        </div>
      </template>
      <template v-else>
        <a-empty style="margin-top: 80px" description="暂无费用信息" />
      </template>
    </div>
    <a-divider style="margin: 0" />
    <div class="fee-summary-footer flex justify-between">
      <div class="footer-text">费用合计</div>
      <div class="footer-value">{{ unitVal(sumFee) }}</div>
    </div>
  </div>
</template>
<script setup>
  const props = defineProps({
    feeForm: Object,
  })
  import { ref, computed, watch } from 'vue'
  import { decimalAddition, unitVal } from '@/utils'
  import { buildTreeData, processTreeData } from './fee-statistic.js'
  // 实时费用信息开始
  const feeRealtimeTreeData = computed(() => {
    let orderFee = props.feeForm.orderFee
    if (orderFee) {
      return buildTreeData(orderFee)
    }
    return []
  })
  // 此处 feeTreeStData 为一个 “ 常量 ”，并未直接将 feeTreeStData 逻辑放到 feeRealtimeTreeData computed 逻辑中
  // 原因是由于费用名字点击展开收起无法操纵 computed 出来的数据，不生效？（存疑）
  const feeTreeStData = ref([])
  watch(
    () => feeRealtimeTreeData.value,
    (newVal) => {
      feeTreeStData.value = processTreeData(newVal, feeTreeStData)
    },
    { immediate: true, deep: true }
  )
  // 实时费用信息结束

  // 费用合计
  const sumFee = computed(() => {
    let sum = 0
    feeTreeStData.value.forEach((item) => {
      sum = decimalAddition(sum, item.value)
    })
    return sum
  })
</script>
<style lang="less" scoped>
  .fee-summary-main {
    padding: 0 12px;
    border: 1px solid #f2f3f5;
    position: relative;
    z-index: 1;
    .fee-summary-content {
      .fee-item {
        .fee-item-field {
          height: 52px;
          line-height: 52px;
          &.children-show {
            height: 46px;
          }
          .fee-name-toggle {
            cursor: pointer;
            .fee-name-toggle-icon {
              margin-left: 2px;
            }
          }
        }
        .fee-item-children {
          background: rgba(247, 248, 250, 0.5);
          padding: 16px;
          margin-bottom: 16px;
          .fee-item-field {
            height: 20px;
            line-height: 20px;
          }
          .fee-item-field + .fee-item-field {
            margin-top: 12px;
          }
        }
      }
    }
  }

  .fee-summary-footer {
    height: 52px;
    line-height: 52px;
    .footer-text {
      font-size: 14px;
      font-weight: 600;
      color: #1d2129;
    }
    .footer-value {
      font-size: 14px;
      font-weight: 600;
      color: rgb(var(--primary-6));
    }
  }
</style>
